package com.swpu.vd.Controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.swpu.commonutils.R;
import com.swpu.servicebase.ExceptionHandler.GuliException;
import com.swpu.vd.Utils.ConstantVodUtils;
import com.swpu.vd.Utils.InitObj;
import com.swpu.vd.service.VodService;
import org.hibernate.validator.constraints.EAN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.DELETE;
import java.io.IOException;
import java.util.*;

/**
 * 功能描述：
 *
 * @Author: ygq
 * @Date: 2021/10/18 20:04
 */
@RestController
@CrossOrigin
@RequestMapping("/eduvod/video")
public class EduVedioController {

    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @PostMapping("/uploadVedio")
    public R uploadVedio(MultipartFile file) throws IOException {
        String VedioId = vodService.uploadfile(file);
        System.out.println(VedioId);
        return R.ok().message("视频上传成功").data("videoId", VedioId);
    }
    //根据视频Id删除阿里云中的视频
    @DeleteMapping("/{id}")
    public R removeById(@PathVariable String id){
        System.out.println("删除的视频ID："+id);
        vodService.remoVedioById(id);
        return R.ok();
    }
    //删除多个阿里云视频
    @DeleteMapping("delete-batch")
    public R deletebatch(@RequestParam("videoIdList") List<String> videoList){

        vodService.deletebatch(videoList);
        return R.ok();
    }
    //根据id获取视频凭证

    @GetMapping("get-play-auth/{videoId}")
    public R getVideoPlayAuth(@PathVariable("videoId") String videoId) throws Exception {

        //获取阿里云存储相关常量
        String accessKeyId = ConstantVodUtils .ACCESS_KEY_ID;
        String accessKeySecret = ConstantVodUtils .ACCESS_KEY_SECRET;

        //初始化对象
        DefaultAcsClient client = InitObj.initVodClient(accessKeyId, accessKeySecret);

        //请求
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoId);

        //响应
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);

        //得到播放凭证
        String playAuth = response.getPlayAuth();

        Map map = new HashMap();
//        Object o = map.get();
        //返回结果
        return R.ok().message("获取凭证成功").data("playAuth", playAuth);

    }

}
